﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI.Design;
using System.ComponentModel.Design;
using System.Web.UI.Design.WebControls;

namespace Ajaxna.Designers
{
    [ToolboxItem(false)]
    public class AjaxnaControlDesigner : ControlDesigner, IServiceProvider
    {
        AjaxnaControl _myControl;
        DesignerActionListCollection _actions;
        DesignerVerbCollection _verbs = new DesignerVerbCollection();
        WindowsFormsEditorService _windowsFormsEditorService;

        public AjaxnaControlDesigner()
        {
            //_verbs.Add(new DesignerVerb("TestString...", this.OnVerb));            
        }

        /*
        protected void OnVerb(object sender, EventArgs e)
        {
            SimpleTextEdit frm = new SimpleTextEdit();
            frm.TextValue = _myControl.TestString;
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                _myControl.TestString = frm.TextValue;

        }
        public override DesignerVerbCollection Verbs
        {
            get
            {                
                return _verbs;
            }
        }
        */

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            if (!(component is AjaxnaControl))
                throw new InvalidOperationException("Component must be of type Ajaxna");

            _myControl = (AjaxnaControl)component;                        
            
        }               

        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (_actions == null)
                {
                    _actions = base.ActionLists;
                    if (null == _actions)
                        _actions = new DesignerActionListCollection();

                    _actions.Add(new AjaxnaControlActionList(_myControl, this));
                }
                return _actions;
            }
        }

        public override string GetDesignTimeHtml()
        {                        
            return CreatePlaceHolderDesignTimeHtml(
                string.Format("You have added <b>{0}</b> scripts.", _myControl.Scripts.Count)
            );            
        }

        
        WindowsFormsEditorService WindowsFormsEditorService
        {
            get
            {
                if (_windowsFormsEditorService == null)
                    _windowsFormsEditorService = new WindowsFormsEditorService();
                return _windowsFormsEditorService;
            }
        }
        

        #region IServiceProvider Members

        public new object GetService(Type serviceType)
        {            
            if (serviceType == typeof(System.Windows.Forms.Design.IWindowsFormsEditorService))
                return this.WindowsFormsEditorService;
            
            return base.GetService(serviceType);
        }

        #endregion
    }
}
